﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;
using Portal.Interface;
using System.Web.Security;

namespace WebPortal
{
    public partial class Login : System.Web.UI.Page
    {
        IOrg _orgSrv = ServiceContainer.Instance().GetService<IOrg>();
        public string Login_UserOrPasswordInvalid = Resources.lang.Login_UserOrPasswordInvalid;
        public string Login_UserRequired = Resources.lang.Login_UserRequired;
        protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack)
            {
                hidReturnUrl.Value = Request.QueryString["ReturnUrl"];
                List<string> strs = _orgSrv.GetDomains();
                ddlDomains.DataSource = strs;
                ddlDomains.DataBind();
                if (strs.Count < 2)
                {
                    ddlDomains.Visible = false;
                    lblDomains.Visible = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text;
            if (user.IndexOf("\\")<0)
            {
                user = ddlDomains.SelectedValue + "\\" + txtUser.Text;
            }

            if (!_orgSrv.CheckUser(user, txtPassword.Text))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ce","checkError();",true);
            }
            else
            {
                Portal.Logic.SSOLogic.SetLoginName(user);
                //string defaultPage = FormsAuthentication.GetRedirectUrl(user, false);
                //Response.Redirect(defaultPage);
                if (!string.IsNullOrEmpty(hidReturnUrl.Value))
                {
                    if (hidReturnUrl.Value.IndexOf("Default.aspx") < 0)
                    {
                        Response.Redirect(hidReturnUrl.Value.Trim());
                    }
                    else
                    {
                        Response.Redirect("Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
    }
}